package com.zhima.panel;

import com.zhima.frame.MainFrame;
import com.zhima.task.GenerateOutDataTask;

import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.zhima.util.StrUtil.addNewLineIfToLong;
import static javax.swing.JFileChooser.APPROVE_OPTION;

/**
 * @author : 芝麻
 * @date : 2023-08-05 13:22
 **/
public class SelectCodePanel extends JPanel {
    private final JButton selectFileJButton;
    private final JLabel selectFilePathJLabel;
    private final JFileChooser jFileChooser;
    private final JTextArea codeJTextArea;
    private final JButton generateOutDataJButton;
    private final ExecutorService executorService = Executors.newFixedThreadPool(MainFrame.AVAILABLE_PROCESSORS);
    private final JLabel baseDirPathJLabel;
    private final JTextField dataBarsNumberJTextField;


    public SelectCodePanel(JTextField dataBarsNumberJTextField, JTextArea codeJTextArea, JLabel baseDirPathJLabel) {
        setLayout(new GridLayout(3, 1));
        this.selectFileJButton = new JButton("选择代码文件");
        this.selectFilePathJLabel = new JLabel("");
        this.codeJTextArea = codeJTextArea;
        this.generateOutDataJButton = new JButton("生成输出数据");
        this.baseDirPathJLabel = baseDirPathJLabel;
        this.dataBarsNumberJTextField = dataBarsNumberJTextField;
        jFileChooser = new JFileChooser();
        jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        Panel h = new Panel(new GridLayout(3, 1));
        h.add(new JLabel("生成输出数据"));
        JPanel kp = new JPanel();
        kp.add(selectFileJButton);
        h.add(kp);
        h.add(new JLabel("当前代码文件路径："));
        add(h);
        add(selectFilePathJLabel);
        JPanel p = new JPanel();
        p.add(generateOutDataJButton);
        add(p);
        initActionListener();
    }

    private void initActionListener() {
        selectFileJButton.addActionListener(e -> {
            int res = jFileChooser.showOpenDialog(this);
            if (res == APPROVE_OPTION) {
                File selectedFile = jFileChooser.getSelectedFile();
                selectFilePathJLabel.setText(addNewLineIfToLong(selectedFile.getAbsolutePath()));
                try (BufferedReader br = new BufferedReader(new FileReader(selectedFile))) {
                    codeJTextArea.setText("");
                    String line;
                    while ((line = br.readLine()) != null) {
                        codeJTextArea.append(line);
                        codeJTextArea.append("\n");
                    }
                } catch (IOException exception) {
                    exception.printStackTrace();
                }
            }
        });
        generateOutDataJButton.addActionListener(e -> {
            if (selectFilePathJLabel.getText().isBlank() || codeJTextArea.getText().isBlank()) {
                JOptionPane.showMessageDialog(null, "请选择代码文件！");
                return;
            }
            executorService.submit(() -> JOptionPane.showMessageDialog(null, "开始生成输出文件，请稍等"));
            executorService.submit(new GenerateOutDataTask(executorService, dataBarsNumberJTextField, baseDirPathJLabel, selectFilePathJLabel.getText()));
        });
    }
}
